const { getUsers, addUser, updateUser } = require('../service/user')
exports.addUser = async function (ctx) {
  let { name, password } = JSON.parse(ctx.request.body)
  let result = await addUser([name, password])

  return result
}

exports.updateUser = async function (ctx) {
  let { name, password } = JSON.parse(ctx.request.body)
  let result = await updateUser([name, password, name])

  return result
}


// api/user/info
exports.info = async function (ctx) {
  await sleep(3)
  return {
    success: true,
    result: [{ name: 'zhangsan', age: 19 }],
    body: ctx.request.body
  }
}

// api/user/info
exports.rows = async function (ctx) {
  // headers: {'Content-Type': 'application/json'}
  // console.log(ctx.request.body)

  const data = await getUsers()

  return {
    success: true,
    result: data, // [{ name: 'Tom', password: '123456' }],
    body: ctx.request.body
  }
}

function sleep(timer = 1) {
  return new Promise(resolve => {
    setTimeout(() => resolve(), timer * 1000)
  })
}